<template>
  <div class="distribution-posters">
	  <img class="logo" src="../../../assets/images/logo2.png" alt="">
    <div class="slider-banner banner">
      <swiper class="swiper-wrapper"  @click="downloadIamge(info.urlCode,'鑫G元.png')" :options="swiperPosters" ref="mySwiper">
        <swiperSlide
          class="swiper-slide"
        >
		<div  @click="downloadIamge(info.urlCode,'鑫G元.png')" >
			
			  <img class="slide-image" :src="info.urlCode"  style="width: 5rem;height: 5rem;display: block;margin: auto;"/>
		</div>
        </swiperSlide>
      </swiper>
    </div>
    <!--<div class="keep bg-color-red" @click="saveImg">保存海报</div>-->
    <div class="preserve acea-row row-center-wrapper" @click="downloadIamge(info.urlCode,'鑫G元.png')">
      <div class="line"></div>
      <div class="tip">点击保存图片</div>
      <div class="line"></div>
    </div>
	
	<div style="text-align: center;margin-top: .2rem;color: black;" @click="copy(info.url)">
		<div>{{info.url}}</div>
		<div>点击复制分享链接</div>
	</div>
  </div>
</template>
<style scoped>
	.distribution-posters{
		position: absolute;
		width: 100%;
		height: 100%;
		/* background: linear-gradient(to top, rgb(244,73,57), rgb(251,177,95)); */
		background-color: white;
	}
	
	.distribution-posters .slider-banner{
		height: auto;
	}
	
	.logo{
		margin: auto;
		margin-top: .9rem;
		width: 2rem;
		height: 2rem;
		display: block;
	}
.preserve {
  color: black;
  text-align: center;
  margin-top: 0.2rem;
}
.preserve .line {
  width: 1rem;
  height: 0.01rem;
  background-color: #fff;
}
.preserve .tip {
  margin: 0 0.3rem;
}
</style>
<script>
	import {Toast} from 'vant'
	import Vue from 'vue'
	Vue.use(Toast)
import { swiper, swiperSlide } from "vue-awesome-swiper";
import "@assets/css/swiper.min.css";
import { getSpreadImg } from "../../../api/user";
export default {
  name: "Poster",
  components: {
    swiper,
    swiperSlide
  },
  props: {},
  data: function() {
    return {
      swiperPosters: {
        speed: 1000,
        effect: "coverflow",
        slidesPerView: "auto",
        centeredSlides: true,
        coverflowEffect: {
          rotate: 0, // 旋转的角度
          stretch: -20, // 拉伸   图片间左右的间距和密集度
          depth: 100, // 深度   切换图片间上下的间距和密集度
          modifier: 3, // 修正值 该值越大前面的效果越明显
          slideShadows: false // 页面阴影效果
        },
        observer: true,
        observeParents: true
      },
      info: [],
      activeIndex: 0
    };
  },
  mounted: function() {
    this.getIndex();
    let that = this;
    this.swiper.on("slideChange", function() {
      that.activeIndex = that.swiper.activeIndex;
    });
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    }
  },
  methods: {
    getIndex: function() {
      let that = this;
      getSpreadImg().then(
        res => {
          that.info = res.data;
		  console.log(res,'1111')
        },
        err => {
          that.$dialog.message(err.msg);
        }
      );
    },
	copy(text){
		console.log(text)
						
		const input = document.createElement('input');
		document.body.appendChild(input);
		input.setAttribute('value', text);
		input.select();
		if (document.execCommand('copy')) {
			document.execCommand('copy');
			Toast("成功复制到剪切板！")
		}
		document.body.removeChild(input);
	},
    downloadIamge: function(imgsrc, name) {
      let image = new Image();
      image.setAttribute("crossOrigin", "anonymous");
      image.onload = function() {
        let canvas = document.createElement("canvas");
        canvas.width = image.width;
        canvas.height = image.height;
        let context = canvas.getContext("2d");
        context.drawImage(image, 0, 0, image.width, image.height);
        let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
        let a = document.createElement("a"); // 生成一个a元素
        let event = new MouseEvent("click"); // 创建一个单击事件
        a.download = name || "photo"; // 设置图片名称
        a.href = url; // 将生成的URL设置为a.href属性
        a.dispatchEvent(event); // 触发a的单击事件
      };
      image.src = imgsrc;
    },
    saveImg: function() {
      console.log(this.info[this.activeIndex].wap_poster);
      this.downloadIamge(
        this.info[this.activeIndex].wap_poster,
        "poster" + this.activeIndex
      );
    }
  }
};
</script>
